package com.letoken.platform.repo.app.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.repo.app.dao.AppUserLockedMapper;
import com.letoken.platform.repo.app.po.AppUserLocked;
import com.letoken.platform.repo.app.redis.AppWalletBlackListRedis;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

/**
 * @version 1.0
 * @author： Christopher
 * @date： 2022/1/6 15:10
 * 锁定用户
 */
@Log4j2
@Repository
public class AppUserLockedManager {

    @Autowired
    private AppUserLockedMapper appUserLockedMapper;


    /**
     * 判断用户是否已锁定
     * @param userId
     * @return
     */
    public boolean isLocked(Integer userId) {
        QueryWrapper<AppUserLocked> query = new QueryWrapper<>();
        query.eq("user_id", userId).eq("locked", 1);
        Integer count = appUserLockedMapper.selectCount(query);
        return count != null && count > 0;
    }

    /**
     * 锁定用户
     * @param userId
     * @param isLocked
     * @return
     */
    public int setUserLocked(Integer userId, boolean isLocked) {
        try {
            QueryWrapper<AppUserLocked> query = new QueryWrapper<>();
            query.eq("user_id", userId);
            AppUserLocked item = appUserLockedMapper.selectOne(query);
            Integer locked = isLocked ? 1 : 0;
            int flag;
            if (item == null) {
                item = new AppUserLocked();
                item.setUserId(userId);
                item.setLocked(locked);
                item.setCreateTime(LocalDateTime.now());
                item.setRemark("系统插入冻结");
                flag = appUserLockedMapper.insert(item);

            } else {
                item.setLocked(locked);
                item.setUpdateTime(LocalDateTime.now());
                item.setRemark("系统更新冻结");
                flag = appUserLockedMapper.updateById(item);
            }
            if (locked == 0){
                AppWalletBlackListRedis.me().del(userId.toString());
            }
            return flag;
        }
        catch (Exception ex){
            return 0;
        }
    }

    /**
     * 锁定用户
     * @param userId
     * @param isLocked
     * @param adminUserId 操作管理员
     * @return
     */
    public int setUserLocked(Integer userId, boolean isLocked,Integer adminUserId) {
        if (adminUserId == null){
            return this.setUserLocked(userId,isLocked);
        }
        try {
            QueryWrapper<AppUserLocked> query = new QueryWrapper<>();
            query.eq("user_id", userId);
            AppUserLocked item = appUserLockedMapper.selectOne(query);
            Integer locked = isLocked ? 1 : 0;
            int flag;
            if (item == null) {
                item = new AppUserLocked();
                item.setUserId(userId);
                item.setLocked(locked);
                item.setOperator(adminUserId);
                item.setCreateTime(LocalDateTime.now());
                item.setRemark("管理员插入冻结");
                flag = appUserLockedMapper.insert(item);
            }else {
                item.setLocked(locked);
                item.setEditedAdmin(adminUserId);
                item.setUpdateTime(LocalDateTime.now());
                item.setRemark("管理员更新冻结");
                flag = appUserLockedMapper.updateById(item);
            }
            if (locked == 0){
                AppWalletBlackListRedis.me().del(userId.toString());
            }
            return flag;
        }
        catch (Exception ex){
            return 0;
        }
    }
}
